package com.volcreate.inn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.volcreate.inn.mapper.BlogMapper;
import com.volcreate.inn.po.Blog;
import com.volcreate.inn.service.IBlogService;
import com.volcreate.inn.utils.MarkdownUtils;
import com.volcreate.inn.vo.BlogQuery;
import com.volcreate.inn.vo.BlogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zxc
 * @since 2020-08-23
 */
@Service
public class BlogService extends ServiceImpl<BlogMapper, Blog> implements IBlogService {


    @Autowired
    private BlogMapper blogMapper;

    @Override
    public Map<String, List<BlogVO>> archiveBlog() {
        List<String> years = blogMapper.selectGroupYear();
        Map<String, List<BlogVO>> map = new HashMap<>();
        for (String year : years) {
            map.put(year, blogMapper.selectArchiveByYear(year));
        }
        return map;
    }

    @Override
    public PageInfo<BlogVO> listBlogVO(int page, int size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(blogMapper.selectAllBlogVO());
    }


    /**
     * 建议分页大一点
     *
     * @param page
     * @param size
     * @param blogQuery
     * @return
     */
    @Override
    public PageInfo<BlogVO> listByBlogQuery(int page, int size, BlogQuery blogQuery) {
        PageHelper.startPage(page, size);

        QueryWrapper<BlogQuery> wrapper = new QueryWrapper<>();
        if (blogQuery != null) {
            if (blogQuery.getTitle() != null) {
                wrapper.like(blogQuery.getTitle() != null, "title", blogQuery.getTitle());
            }
            if (blogQuery.isRecommendable()) {
                wrapper.eq("recommendable", blogQuery.isRecommendable());
            }
            if (blogQuery.getTypeId() != null) {
                wrapper.eq("type_id", blogQuery.getTypeId());
            }
            if (blogQuery.getTag() != null) {
                wrapper.inSql("b.id", "select bt.blog_id from voldb.v_blog_tag bt where tag_id = " + blogQuery.getTag().getId());
            }
            if (blogQuery.getSortBy() != null) {
                wrapper.orderByDesc(blogQuery.getSortBy());
            }
        }
        wrapper
                .eq("deleted", 0);
        return new PageInfo<>(blogMapper.selectByBlogQuery(wrapper));
    }

    @Override
    public BlogVO getOne(Long id) {
        return blogMapper.selectById(id);
    }

    @Override
    public boolean save(BlogVO blogVO) {
        int insert = blogMapper.insert(blogVO);
        int i = blogMapper.insertBatchBlogTag(blogVO.getId(), blogVO.getTags());
        return insert >= 1;
    }

    @Override
    public boolean updateLike(Long id) {
        return blogMapper.updateLike(id) > 0;
    }

    @Override
    public boolean updateById(Long id, BlogVO blogVO) {
        if (blogVO.getTags().size() != 0) {
            blogMapper.deleteBlogTag(id);
            blogMapper.insertBatchBlogTag(id, blogVO.getTags());
        } else {
            blogMapper.deleteBlogTag(id);
        }
        int i = blogMapper.updateById(blogVO);
        return i >= 1;
    }

    @Override
    public boolean deleteById(Long id) {
        int i = blogMapper.deleteBlogTag(id);
        blogMapper.deleteById(id);
        return i >= 1;
    }

    @Override
    @Transactional
    public BlogVO getAndConvert(Long id) {
        BlogVO blogVO = blogMapper.selectById(id);
        if (blogVO == null) {
            throw new RuntimeException("博客不存在");
        }
        String s = MarkdownUtils.markdownToHtmlExtensions(blogVO.getContent());
        blogVO.setContent(s);

        // 调用此方法的都是在查看博客内容的,此时浏览数+1
        blogMapper.updateViews(id);
        return blogVO;
    }


    public PageInfo<BlogVO> listByBlogSearch(int page, int size, String query) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(blogMapper.selectByBlogSearch(query));
    }

    public PageInfo<BlogVO> listBlogByUser(int page, int size, Long userId) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(blogMapper.selectBlogByUser(userId));
    }



}
